# -*- coding: utf-8 -*-
'''
@File    :   capability_3_py.py
@Time    :   2025/09/05 09:11:00
@Author  :   LX
@Version :   1.0.0
@Desc    :   使用py生成html页面做ui


# 启动服务
python simple_by_py.py  server


'''

import os , sys
# 获取当前文件所在目录的上两级目录（即 src/common）
current_dir = os.path.dirname(os.path.abspath(__file__))
common_dir = os.path.abspath(os.path.join(current_dir,'..', '..','src'))
print(common_dir)
sys.path.append(common_dir)


from pycapabilitydecorator  import capability

# ! dominate  会内存泄漏 使用 yattag
from yattag import Doc


'''
使用py生成html页面做ui

# 启动服务
python capability_3_py.py server




py生成页面:
http://127.0.0.1:5000/capability/capability_3_py/pages/home.html

http://127.0.0.1:5000/capability/capability_3_py/pages/about.html

http://127.0.0.1:5000/capability/capability_3_py/pages/contact.html





'''
@capability.option(
    pages = [
             "home.html",
             "about.html" ,
             "contact.html"
            ],
    description="py生成的html页面作为返回页面"
)
def home(params):
    """生成页面"""
    doc, tag, text = Doc().tagtext()
    with tag('html'):
        with tag('head'):
            with tag('link', type="text/css" , rel='stylesheet',href='./style.css'):
                pass
            with tag('script', type="text/javascript" , src='./script.js') :
                pass
            with tag('title'):
                if params.get("page_path") == 'home.html':
                    text('首页')
                elif params.get("page_path") == 'about.html':
                    text('关于')
                elif params.get("page_path") == 'contact.html':
                    text('联系我们')
        with tag('body'):
            with tag('div'):
               with tag('ul'):
                    for i in ['home', 'about', 'contact']:
                        with tag('li'):
                            with tag('a',href='./%s.html' % i):
                                text(i)
            with tag('p'):
                text(f'params: {params}')

    return doc.getvalue()

@capability.option(
    pages = [ "style.css" ],
    description="py生成 css 返回"
)
def css(params):
    print("===style params:", params)
    return str("p{ color: red; }")



@capability.option(
    pages = [ "script.js" ],
    pages_type = "application/javascript",
    description="py生成 js 返回"
)
def js(params):
    print("===js params:", params)
    return str(f"alert(\"hello js!{params}\");")


# ---- 最简单能力接入 带入参
@capability.option()
def function1(params):
    return {
        "code": 0,
        "message": f"[function1]成功,入参{params}"
    }
